// me/fs/dir_exist_demo.js

const fs = require('fs');
const path = require('path');

// 判断目录是否存在，返回 Promise
const dirExist = (dir) => {
    return new Promise((resolve) => {
        fs.access(dir, fs.constants.F_OK, (err) => {
            if (err) {
                resolve(false);
            } else {
                resolve(true);
            }
        })
    });
};

// 测试目录是否存在
const testDirExist = () => {
    
    const fn = (dir) => {
        dirExist(dir).then((exist) => {
            if (exist) {
                console.log(`${dir} 目录存在`)
            } else {
                console.log(`${dir} 目录不存在`)
            }
        })
    };

    fn(path.join(__dirname, '../event'))

    fn(path.join(__dirname, '../event1/a'))
    
};

// 目录不存在时创建目录
const test2 = () => {
    const dir = 'F:\\test2\\a\\b';//path.join(__dirname, '../event1/a');
    dirExist(dir).then((exist) => {
        if (!exist) {
            fs.mkdirSync(dir, {recursive:true});
        }
    }).then(() => {
        console.log('done')
    })
};

function main() {
    // testDirExist();
    test2();
}

main();
